﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;
using System.Web.Mail;

/// <summary>
/// CMobileExecute의 요약 설명입니다.
/// </summary>
public class CMobileExecute : CModelBase
{
    public bool InsertMember(string companyname, string name, string email)
    {
        if (companyname == "" || name == "" || email == "")
            throw (new Exception("필수 항목이 빠져있습니다."));

        // 업무 수행
        GetConnection();

        try
        {
            // 회원 정보 등록
            string sql = "INSERT INTO MEMBER(companyname, name, email) VALUES('" + MRF(companyname) + "','" + MRF(name) + "','" + MRF(email) + "')";
            ExecuteSQL(sql);

            // 이메일 보내기
            string mailcontent = ReadTextFile(HttpContext.Current.Server.MapPath("/email/mail.html"));
            mailcontent = mailcontent.Replace("[[COMPANYNAME]]", companyname);
            mailcontent = mailcontent.Replace("[[NAME]]", name);
            mailcontent = mailcontent.Replace("[[EMAIL]]", email);

            MailMessage _mail = new MailMessage();
            _mail.From = email;

            //_mail.To = "donus@crevisse.com";
            _mail.To = "seonah.yoon@crevisse.com";

            _mail.Subject = "[모바일 소개자료 신청]";

            _mail.Body = mailcontent;
            _mail.BodyFormat = System.Web.Mail.MailFormat.Html;

            SmtpMail.Send(_mail);

        }
        finally
        {
            ReleaseConnection();
        }

        return true;
    }

    public bool UpdateMember(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string memberidx = CheckNull(param["memberidx"]);

        // 업무 수행
        GetConnection();

        try
        {
            string sql = "update MEMBER set IsSendMail='1' where MemberIdx in (" + memberidx + ")";
            ExecuteSQL(sql);
        }
        finally
        {
            ReleaseConnection();
        }

        return true;
    }

    public bool DeleteMember(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string memberidx = CheckNull(param["memberidx"]);

        // 업무 수행
        GetConnection();

        try
        {
            string sql = "DELETE FROM MEMBER WHERE MemberIdx in (" + memberidx + ")";
            ExecuteSQL(sql);
        }
        finally
        {
            ReleaseConnection();
        }

        return true;
    }

    public bool UpdateFeedVisible(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string newsidx = CheckNull(param["newsidx"]);
        string status = CheckNull(param["status"]);

        // 업무 수행
        GetConnection();

        try
        {
            string sql = "update NEWS set status='" + status + "' where NewsIdx in (" + newsidx + ")";
            ExecuteSQL(sql);
        }
        finally
        {
            ReleaseConnection();
        }

        return true;
    }

    public bool DeleteFeed(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string newsidx = CheckNull(param["newsidx"]);

        // 업무 수행
        GetConnection();

        try
        {
            string sql = "DELETE FROM NEWS WHERE NewsIdx in (" + newsidx + ")";
            ExecuteSQL(sql);
        }
        finally
        {
            ReleaseConnection();
        }

        return true;
    }

    public bool InsertOrgan(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string organname = CheckNull(param["organname"]);
        string organcode = CheckNull(param["organcode"]);
        string url = CheckNull(param["url"]);

        if (organname == "" || organcode == "" || url == "")
            throw (new Exception("필수 항목이 빠져있습니다."));

        // 업무 수행
        GetConnection();

        try
        {
            string sql = "INSERT INTO ORGAN (OrganName, OrganCode, URL) VALUES ('" + organname + "', '" + organcode + "', '" + url + "')";
            ExecuteSQL(sql);
        }
        finally
        {
            ReleaseConnection();
        }

        return true;
    }

    public bool UpdateOrgan(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string organidx = CheckNull(param["organidx"]);
        string organname = CheckNull(param["organname"]);
        string organcode = CheckNull(param["organcode"]);
        string url = CheckNull(param["url"]);

        if (organidx == "" || organname == "" || organcode == "" || url == "")
            throw (new Exception("필수 항목이 빠져있습니다."));

        // 업무 수행
        GetConnection();

        try
        {
            string sql = "UPDATE ORGAN SET organname = '" + MRF(organname) + "', organcode = '" + MRF(organcode) + "', url = '" + MRF(url) + "' WHERE organidx = '" + organidx + "' ";
            ExecuteSQL(sql);
        }
        finally
        {
            ReleaseConnection();
        }

        return true;
    }

    public bool DeleteOrgan(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string organidx = CheckNull(param["organidx"]);

        // 업무 수행
        GetConnection();

        try
        {
            string sql = "DELETE FROM ORGAN WHERE OrganIdx in (" + organidx + ")";
            ExecuteSQL(sql);
        }
        finally
        {
            ReleaseConnection();
        }

        return true;
    }
}
